package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：众包_微信_注册_controller
 */
@Controller
@RequestMapping("/zhongbao/weixin/register")
public class ZhongBaoWeiXinRegisterController extends BaseController {

    @Autowired
    private SundayWeiXinMemberService personService;


    /**
     * 注册_个人_第一步
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/person/step1", method = RequestMethod.GET)
    @ApiOperation(value = "注册_个人_第一步")
    public String person_step1(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ) {
        try {
            return "/zhongbao/weixin/register/step1_person";
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/zhongbao/weixin/register/person/step1----e=" + e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/wexin/error";
        }
    }

    /**
     * 注册_商户_第一步
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/business/step1", method = RequestMethod.GET)
    @ApiOperation(value = "注册_商户_第一步")
    public String business_step1(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ) {
        try {
            return "/zhongbao/weixin/register/step1_business";
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/zhongbao/weixin/register/business/step1----e=" + e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/wexin/error";
        }
    }

    /**
     * 提交注册
     *
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiOperation(value = "【微信版】提交注册信息")
    public void submit(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
            @ApiParam(required = true, name = "nickName", value = "姓名") @RequestParam(value = "nickName", required = true) String nickName,
            @ApiParam(required = true, name = "mobile", value = "手机号码") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = false, name = "trade", value = "行业") @RequestParam(value = "trade", required = false) String trade,
            @ApiParam(required = false, name = "career", value = "职位") @RequestParam(value = "receiveMobile", required = false) String career,
            @ApiParam(required = false, name = "provinceId", value = "省ID。管理版传入") @RequestParam(value = "provinceId", required = false) Long provinceId,
            @ApiParam(required = false, name = "provinceName", value = "省名称。管理版传入") @RequestParam(value = "provinceName", required = false) String provinceName,
            @ApiParam(required = false, name = "cityId", value = "市ID。管理版传入") @RequestParam(value = "cityId", required = false) Long cityId,
            @ApiParam(required = false, name = "cityName", value = "市名称。管理版传入") @RequestParam(value = "cityName", required = false) String cityName,
            @ApiParam(required = false, name = "districtId", value = "区县ID。管理版传入") @RequestParam(value = "districtId", required = false) Long districtId,
            @ApiParam(required = false, name = "districtName", value = "区县名称。管理版传入") @RequestParam(value = "districtName", required = false) String districtName,
            @ApiParam(required = false, name = "company", value = "公司") @RequestParam(value = "company", required = false) String company,
            @ApiParam(required = false, name = "mobileName", value = "联系人") @RequestParam(value = "mobileName", required = false) String mobileName,
            @ApiParam(required = true, name = "userType", value = "个人类型") @RequestParam(value = "userType", required = true) Integer userType
    ) {
        try {
            // SundayMemberSessionDto personSessionDto = ;
            personService.register(request, AccountIdHoldUtils.getSessionMember(request).getId(), code, nickName, mobile, trade, career,
                    provinceId, provinceName, cityId, cityName, districtId, districtName, company,
                    mobileName, userType);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.info("/zhongbao/weixin/register--------" + e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }
    /**
     *
     */
    /**
     * 员工入口
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/employee/step1", method = RequestMethod.GET)
    @ApiOperation(value = "企业员工注册")
    public String employee_index(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model
    ) {
        try {
            return "/zhongbao/weixin/register/step1_employee";
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/zhongbao/weixin/register/employee/step1----e=" + e.getMessage());

            }
            e.printStackTrace();
            return "/zhongbao/wexin/error";

        }
    }
    /**
     * 员工注册_提交信息
     *
     * @param request
     * @param response
     * @param
    @RequestMapping(value = "/employee/submit", method = RequestMethod.POST)
    @ApiOperation(value = "员工注册_提交信息")
    public void employee_submit(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "code", value = "验证码") @RequestParam(value = "code", required = true) String code,
            @ApiParam(required = true, name = "mobile", value = "手机号码") @RequestParam(value = "mobile", required = true) String mobile
           ) {
        try {
            // SundayMemberSessionDto personSessionDto = ;
            SundayMember parent=personService.register_employee(request,AccountIdHoldUtils.getSessionMember(request).getId(), mobile, code);
            ResponseMessage.success(parent);
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.info("/zhongbao/weixin/register/employee/submit--------" + e);
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }*/
}